package com.yao;

import com.yao.mapper.PersonMapper;
import com.yao.pojo.Person;
import com.yao.pojo.PersonExample;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * Copyright (C), 2022-2023, 姚兵
 * Author: 32210
 * Date: 2023/2/17 20:14
 * FileName: MybatisLearn
 * Description:
 */
@Slf4j
public class MybatisLearn {
    static SqlSessionFactory factory =null;
    static {
        InputStream is = null;
        try {
            is = Resources.getResourceAsStream("mybatis.xml");
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            factory = builder.build(is);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
    public static void main(String[] args) throws IOException, SQLException {

        SqlSession sqlSession = factory.openSession();
        testInsert(sqlSession);
        testSelectList(sqlSession);
    }

    public static void testSelectList(SqlSession sqlSession){
        PersonMapper mapper = sqlSession.getMapper(PersonMapper.class);
        PersonExample personExample=new PersonExample() ;
        log.info("{}",mapper.selectByExample(personExample));
    }
    public static void testInsert(SqlSession sqlSession){
        PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
        Person p = new Person();
        p.setName("haha");
        p.setAge(22);
        p.setGender(11);
        int result = personMapper.insert(p);
        log.info("我是{}",result);
        sqlSession.commit();
    }
}
